using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using CommunityServer.Spam;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using Telligent.DynamicConfiguration.Components;

namespace CS.Spam
{
    public class TweetModuleRule : BlogSpamRule
    {
        #region Members
        private string _description = "This is not actually a spam rule.  It is used to configure the CS Twitter Module.";
        private string _name = "Blog Post Twitter Module";
        private Guid _ruleId = new Guid("54B9C0E5-43B9-3de1-87AC-C12CB85FDDD4");

        private string _moduleUser = "CSTweetModule";
        private string _modulePassword = "wh0c@ngue$$";
        #endregion

        #region Properties
        public override string Description
        {
            get{  return _description; }
        }

        public override string Name
        {
            get{return _name;}
        }

        public override Guid RuleID
        {
            get{return _ruleId; }
        }

        // only run on first pass
        public override PassCriteria PassCriteria
        {
            get{ return PassCriteria.FirstPass; }
        }
 


        public string Username
        {
            get { return GetStringValue("username", string.Empty); }
        }
        public string Password
        {
            get { return GetStringValue("password", string.Empty); }
        }
        public string ModuleUser
        {
            get { return _moduleUser; }
        }
        public string ModulePassword
        {
            get { return _modulePassword; }
        }
        public string TweetMessage
        {
            get { return GetStringValue("tweetMessage", string.Empty); }
        }
        public bool TweetExternal
        {
            get { return GetBoolValue("tweetExternal", false); }
        }
        public bool Broadcast
        {
            get { return GetBoolValue("broadcast", true); }
        }
        public bool EnableModule
        {
            get { return IsEnabled(); }
        }

        #endregion

        public override int CalculateSpamScore(WeblogPost post, CSPostEventArgs e)
        {
            // ha - this isn't a spam rule at all
            int rtn = 0;
            return rtn;
        }

        public override PropertyGroup[] GetAvailablePropertyGroups()
        {
            // assign rule instance to post tweeter
            if (PostTweeter.Instance.Rule == null) { PostTweeter.Instance.Init(this); }

            PropertyGroup[] propertyGroups = new PropertyGroup[] { 
                new PropertyGroup("spamOptions", "Spam Settings", 1)
            };

            propertyGroups[0].Properties.Add(new Property("username", "Your Twitter Username", 
                PropertyType.String, 0, string.Empty));
            propertyGroups[0].Properties.Add(new Property("password", "Your Twitter Password", 
                PropertyType.String, 1, string.Empty));

            propertyGroups[0].Properties.Add(new Property("broadcast", @"
If you don't want to send a Tweet
to the CSTweetModule user on Twitter, 
enter 'false'.", PropertyType.Bool, 2, "true"));

            propertyGroups[0].Properties.Add(new Property("tweetMessage",@"
How your post tweet should be formatted. 
'{0}' will be replaced by the post title.  
'{1}' will be replaced by the post excerpt.  
'{2}' will be replaced by a link to the post.", PropertyType.String, 3, "(>- {0}: {1}.. {2}" ));

            propertyGroups[0].Properties.Add(new Property("tweetExternal", @"
Whether to tweet external blog posts. 
If this is 'true' then every new post, 
including from mirrored blogs, 
will be sent to Twitter.", PropertyType.Bool, 4, "false"));

            return propertyGroups;
        }

//        public override ArrayList  GetAvailableSettings()
//        {
//            // assign rule instance to post tweeter
//            if (PostTweeter.Instance.Rule == null) { PostTweeter.Instance.Init(this); }

//            // create settings
//            ArrayList list = new ArrayList();
//            //list.Add(new RuleSetting(_ruleId, "enable", "Enable Post Tweet Module", "true", 5));
//            list.Add(new RuleSetting(_ruleId, "username", "Your Twitter Username", string.Empty, 20));
//            list.Add(new RuleSetting(_ruleId, "password", "Your Twitter Password", string.Empty, 20));
//            list.Add(new RuleSetting(_ruleId, "broadcast", @"
//If you don't want to send a Tweet
//to the CSTweetModule user on Twitter, 
//enter 'false'.", "true", 5));
//            list.Add(new RuleSetting(_ruleId, "tweetMessage", @"
//How your post tweet should be formatted. 
//'{0}' will be replaced by the post title.  
//'{1}' will be replaced by the post excerpt.  
//'{2}' will be replaced by a link to the post.", "(>- {0}: {1}.. {2}", 140));
//            list.Add(new RuleSetting(_ruleId, "tweetExternal", @"
//Whether to tweet external blog posts. 
//If this is 'true' then every new post, 
//including from mirrored blogs, 
//will be sent to Twitter.", "false", 5));
//            return list;
//        }

        #region Helper functions

        protected bool IsEnabled()
        {
            bool rtn = false;
            ArrayList rules = SpamRules.GetSelectedRules();
            if (rules != null)
            {
                foreach (SpamRule rule in rules)
                {
                    if (rule.RuleID == this.RuleID) { return true; }
                }
            }
            return rtn;
        }
        #endregion
    }
}
